package com.toefl.speaking.business;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.toefl.speaking.dao.SQLiteHelper;
import com.toefl.speaking.dao.bean.AudioInfo;
import com.toefl.speaking.dao.bean.QuestionUserBean;
import com.toefl.speaking.exception.Logger;
import com.toefl.speaking.schema.SchemaFieldConstants.QuestionFields;
import com.toefl.speaking.schema.SchemaFieldConstants.QuestionUserFields;
import com.toefl.speaking.util.StringUtils;

public class QuestionUserBusiness {

	private static final String TAG = "QuestionUserBusiness";

	// select * from policy_customer where cntrct_no=? and is_owner =?
	private static final String SQL_QUESTIONINFO_BY_ID = "select * from " + QuestionUserFields.TABLE_NAME + " where "
			+ QuestionUserFields.QUESTION_ID + "=? and " + QuestionUserFields.USER_ID + "=?";
	private static final String SQL_QUESTIONINFO_BY_TYPE = "select * from " + QuestionUserFields.TABLE_NAME + " uq,"
			+ QuestionFields.TABLE_NAME + " q " + " where  q.question_id = uq.question_id and "
			+ QuestionFields.QUESTION_TYPE + "=? and uq.user_id = ?";

	/**
	 * @param question_id
	 * @return
	 */
	public static QuestionUserBean getQuestionInfoByQID(final String question_id, final String user_id) {
		if (StringUtils.isEmpty(question_id) & StringUtils.isEmpty(user_id)) {
			return null;
		}
		QuestionUserBean questionUserBean = null;
		SQLiteDatabase db = null;
		Cursor cursor = null;
		try {
			db = SQLiteHelper.getReadableDatabase();
			if (db != null) {
				cursor = db.rawQuery(SQL_QUESTIONINFO_BY_ID, new String[] { question_id, user_id });
				if (cursor != null && cursor.moveToNext()) {
					questionUserBean = populateQuestionUserInfo(cursor);
				}
			}
		} finally {
			SQLiteHelper.closeDb(db, cursor);
		}
		return questionUserBean;
	}

	/**
	 * 我的录音列表
	 * @param question_type
	 * @param user_id
	 * @return
	 */
	public static ArrayList<AudioInfo> getQuestionInfoByQuestionType(final String question_type, final String user_id) {
		if (StringUtils.isEmpty(question_type) & StringUtils.isEmpty(user_id)) {
			return null;
		}
		ArrayList<AudioInfo> audioInfoInfo = new ArrayList<AudioInfo>();
		SQLiteDatabase db = null;
		Cursor cursor = null;
		try {
			db = SQLiteHelper.getReadableDatabase();
			if (db != null) {
				// db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
				cursor = db.rawQuery(SQL_QUESTIONINFO_BY_TYPE, new String[] { question_type, user_id });
				if (cursor != null) {
					int a = cursor.getCount();
					while (cursor.moveToNext()) {
						audioInfoInfo.add(populateAudioInfo(cursor));
					}
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			SQLiteHelper.closeDb(db, cursor);
		}
		return audioInfoInfo;
	}

	/**
	 * 保存录音
	 * @param qustionUser
	 * @return
	 */
	public static boolean saveAudio(QuestionUserBean qustionUser) {
		SQLiteDatabase db = null;
		long recordId = 0;
		try {
			db = SQLiteHelper.getWritableDatabase();
			if (db != null) {
				ContentValues values = new ContentValues();
				values.put(QuestionUserFields.QUESTION_ID, qustionUser.question_id);
				values.put(QuestionUserFields.USER_ID, qustionUser.uesr_id);
				values.put(QuestionUserFields.QUESTION_USER_AUDIO_PATH, qustionUser.question_user_audio_path);
				values.put(QuestionUserFields.QUESTION_REWARD_AUDIO_PATH, qustionUser.question_reward_audio_path);
				recordId = 0;
				recordId = db.insert(QuestionUserFields.TABLE_NAME, "", values);
			}
		} catch (Exception ex) {
			Logger.printStackTrace(TAG, ex);
		} finally {
			SQLiteHelper.closeDb(db, null);
		}
		return recordId > 0L;
	}

	private static QuestionUserBean populateQuestionUserInfo(Cursor cursor) {
		QuestionUserBean questionUser = new QuestionUserBean();
		questionUser.question_id = cursor.getInt(cursor.getColumnIndex(QuestionUserFields.QUESTION_ID));
		questionUser.uesr_id = cursor.getInt(cursor.getColumnIndex(QuestionUserFields.USER_ID));
		questionUser.question_user_audio_path = cursor.getString(cursor
				.getColumnIndex(QuestionUserFields.QUESTION_USER_AUDIO_PATH));
		questionUser.question_reward_audio_path = cursor.getString(cursor
				.getColumnIndex(QuestionUserFields.QUESTION_REWARD_AUDIO_PATH));
		return questionUser;
	}

	private static AudioInfo populateAudioInfo(Cursor cursor) {
		AudioInfo audioInfo = new AudioInfo();
		audioInfo.questionBean.question_id = cursor.getInt(cursor.getColumnIndex(QuestionFields.QUESTION_ID));
		audioInfo.questionBean.question_content = cursor.getString(cursor
				.getColumnIndex(QuestionFields.QUESTION_CONTENT));
		audioInfo.questionBean.question_title = cursor.getString(cursor.getColumnIndex(QuestionFields.QUESTION_TITLE));
		audioInfo.questionBean.question_type = cursor.getInt(cursor.getColumnIndex(QuestionFields.QUESTION_TYPE));
		audioInfo.questionBean.question_listent_path = cursor.getString(cursor
				.getColumnIndex(QuestionFields.QUESTION_LISTENINT_PATH));
		audioInfo.questionUserBean.uesr_id = cursor.getInt(cursor.getColumnIndex(QuestionUserFields.USER_ID));
		audioInfo.questionUserBean.question_id = cursor.getInt(cursor.getColumnIndex(QuestionUserFields.QUESTION_ID));
		audioInfo.questionUserBean.question_user_audio_path = cursor.getString(cursor
				.getColumnIndex(QuestionUserFields.QUESTION_USER_AUDIO_PATH));
		audioInfo.questionUserBean.question_reward_audio_path = cursor.getString(cursor
				.getColumnIndex(QuestionUserFields.QUESTION_REWARD_AUDIO_PATH));
		return audioInfo;
	}

}
